

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;

/**
 * @author 绛河
 * 登录页面
 */
public class Login {
    public static void main(String[] args) {
        new Register();
    }
}

class Register {
    DatabaseConnection dc = new DatabaseConnection();
    Font font = new Font("仿宋", Font.BOLD, 20);
    //定义一个参数记录登录的账户
    public static String loginInformation;

    public static String getLoginInformation() {
        return loginInformation;
    }

    public static void setLoginInformation(String loginInformation) {
        Register.loginInformation = loginInformation;
    }

    JFrame page1;
    JLabel textIcon1, textIcon2, textIcon3, textIcon4;
    JTextField entryField1;
    JPasswordField entryField2;
    JButton button1, button3;
    JCheckBox checkBox;
    String data;

    public Register() {
        page1 = new JFrame("系统登录窗口");
        page1.setSize(650, 500);
        page1.setLocationRelativeTo(null);//设置窗口每次启动都显示在屏幕正中央
        page1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        page1.setLayout(null);
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (Exception e) {
            System.out.println(e);
        }
        JComboBox<String> dropBox = new JComboBox<>();
        dropBox.addItem("身份");
        dropBox.addItem("学生");
        dropBox.addItem("教师");
        dropBox.addItem("管理员");
        dropBox.setBounds(270, 110, 180, 40);
        dropBox.setFont(font);
        textIcon4 = new JLabel("角  色:");
        textIcon4.setBounds(160, 110, 100, 40);
        textIcon4.setFont(font);
        //为JComboBox下拉框组件注册动作监听器
        dropBox.addActionListener(e -> {
            data = (String) dropBox.getSelectedItem();
        });
        textIcon3 = new JLabel("欢迎来到学生管理系统");
        textIcon3.setBounds(210, 10, 500, 40);
        textIcon3.setFont(font);
        textIcon1 = new JLabel("账  号：");
        textIcon1.setBounds(160, 160, 100, 40);
        textIcon1.setFont(font);
        entryField1 = new JTextField(null, 20);
        entryField1.setBounds(270, 160, 180, 30);
        entryField1.setFont(font);
        textIcon2 = new JLabel("密  码：");
        textIcon2.setBounds(160, 210, 100, 40);
        textIcon2.setFont(font);
        entryField2 = new JPasswordField(null, 20);
        entryField2.setBounds(270, 210, 180, 30);
        entryField2.setEchoChar('*');//设置密码的外显为*
        entryField2.setFont(font);
        checkBox = new JCheckBox("显示密码");
        checkBox.setBounds(360, 320, 80, 40);
        checkBox.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                if (e.getStateChange() == ItemEvent.SELECTED) {//被选中
                    entryField2.setEchoChar((char) 0);
                } else {
                    entryField2.setEchoChar('*');
                }
            }
        });

        ActionListener listener = new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {

            }
        };
        checkBox.addActionListener(listener);
        button1 = new JButton("登录");
        button1.setBounds(210, 260, 80, 40);
        //匿名内部类
        button1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                Statement st = null;
                Connection con = null;
                ResultSet rs = null;
                try {
                    con = dc.getConnection();
                    String sql = null;
                    if (data.equals("学生")) {
                        sql = "select * from student_message";//from 后跟表名
                    } else if (data.equals("教师")) {
                        sql = "select * from teacher_message";
                    } else if (data.equals("管理员")) {
                        sql = "select * from administrator";
                    }

                    st = con.createStatement();
                    rs = st.executeQuery(sql);
                    boolean flag = false;
                    while (rs.next()) {
                        //如果输入的用户名和密码与数据库中的用户和对应的密码相同，则弹出“登录成功！”的窗口
                        if (data.equals("学生")) {
                            if (entryField1.getText().equals(rs.getString(1)) && entryField2.getText().equals(rs.getString(4))) {//学生账户登录，转到学生能够拥有的功能的页面上
                                JOptionPane.showMessageDialog(null, "登录成功！");
                                Register.setLoginInformation(entryField1.getText());
                                new FunctionStudent().student();
                                flag = true;//登陆成功后将标记改为true方便确认
                                page1.dispose();//关闭窗体，释放所有资源
                                break;
                            }
                        }
                        if (data.equals("教师")) {


                            if (entryField1.getText().equals(rs.getString(1)) && entryField2.getText().equals(rs.getString(4))
                            ) {//教师账户登录，转到教师能够拥有的功能的页面上
                                JOptionPane.showMessageDialog(null, "登录成功！");
                                loginInformation = entryField1.getText();
                                new FunctionTeacher().teacher();
                                flag = true;//登陆成功后将标记改为true方便确认
                                page1.dispose();//关闭窗体，释放所有资源
                                break;
                            }
                        }
                        if (data.equals("管理员")) {
                            if (entryField1.getText().equals(rs.getString(1)) && entryField2.getText().equals(rs.getString(3))
                            ) {//教师账户登录，转到教师能够拥有的功能的页面上
                                JOptionPane.showMessageDialog(null, "登录成功！");
                                loginInformation = entryField1.getText();
                                new FunctionAdministrator().administrator();
                                flag = true;//登陆成功后将标记改为true方便确认
                                page1.dispose();//关闭窗体，释放所有资源
                                break;
                            }
                        }
                    }
                    if (flag == false) {//如果标记为false，则表示用户名和密码在数据库中未找到，弹出“登录失败！请重新输入！”的窗口
                        JOptionPane.showMessageDialog(null, "登录失败！请重新输入！");
                        entryField1.setText(null);//清空账号栏
                        entryField2.setText(null);//清空密码栏
                    }
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
        });
        button1.setFont(font);
        button3 = new JButton("注册");
        button3.setBounds(340, 260, 80, 40);
        button3.setFont(font);
        button3.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (data.equals("学生")) {
                    new Enroll().studentSignIn();
                    page1.dispose();
                } else if (data.equals("教师")) {
                    new Enroll().teacherSignIn();
                    page1.dispose();
                } else {
                    new Enroll().administratorRegistration();
                    page1.dispose();
                }
            }
        });

        //将这些按钮和文本等加入到窗体中
        page1.add(textIcon1);
        page1.add(entryField1);
        page1.add(textIcon2);
        page1.add(entryField2);
        page1.add(textIcon3);
        page1.add(button1);
        page1.add(checkBox);
        page1.add(textIcon4);
        page1.add(button3);
        page1.add(dropBox);
        page1.setVisible(true);//让组件显示
    }
}